﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using The_Last_Knights.Helper;

namespace The_Last_Knights.Event
{
    public class EventEntity: IEvent
    {
        public static Game _Game;

        protected List<string> _mustFinished;
        protected List<string> _mustRunning;
        protected List<string> _mustNotRun;

        public List<string> MustFinished
        {
            get { return _mustFinished; }
            set { _mustFinished = value; }
        }

        public List<string> MustRunning
        {
            get { return _mustRunning; }
            set { _mustRunning = value; }
        }

        public List<string> MustNotRun
        {
            get { return _mustNotRun; }
            set { _mustNotRun = value; }
        }

        public EventEntity()
        {
            _mustFinished = new List<string>();
            _mustNotRun = new List<string>();
            _mustRunning = new List<string>();
        }

        public EventEntity(List<string> mustFinished, List<string> mustRunning, List<string> mustNotRun)
        {
            _mustFinished = mustFinished;
            _mustRunning = mustRunning;
            _mustNotRun = mustNotRun;
        }        

        public virtual bool CheckPreCondition()
        {
            bool flag = true;
            foreach (var VARIABLE in _mustFinished)
            {
                flag = flag &&
                       (EventRoutingManager.GetInstance(_Game).GetEventState(VARIABLE) ==
                        EventRoutingManager.EVENTSTATE.FINISHED);
            }
            if (!flag)
                return false;
            foreach (var VARIABLE in _mustRunning)
            {
                flag = flag &&
                       (EventRoutingManager.GetInstance(_Game).GetEventState(VARIABLE) ==
                        EventRoutingManager.EVENTSTATE.RUNNING);
            }

            if (!flag)
                return flag;

            foreach (var VARIABLE in _mustNotRun)
            {
                flag = flag &&
                       (EventRoutingManager.GetInstance(_Game).GetEventState(VARIABLE) ==
                        EventRoutingManager.EVENTSTATE.NOTRUN);
            }
            return flag;
        }

        public virtual bool OnEvent(object source, object sender, string eventId, object wParam, object lParam)
        {
            if (CheckPreCondition())
                return Execute(source, sender, eventId, wParam, lParam);
            return false;
        }

        public virtual bool Execute(object source, object sender, string eventId, object wParam, object lParam)
        {
            return false;
        }

        public virtual void Update(GameTime gameTime)
        {
            
        }
    }
}
